package holdtime.xlxc.activities.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import holdtime.xlxc.R;
import holdtime.xlxc.base.BaseActivity;
import holdtime.xlxc.tools.util.SharedPreferencesUtil;

public class MainActivity extends BaseActivity {

    private int number = 0;
    private AutoVerticalScrollTextView verticalScrollTV;
    private boolean isRunning=true;
    private String[] strings={"学车必不可少的神器！", "学时查询，实时查看练车记录"};

    @Override
    protected void onNewIntent(Intent intent) {//activity为singletask时，只有一个实例，添加此方法。
        super.onNewIntent(intent);
        setIntent(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        actionBarTitle = getResources().getString(R.string.app_name);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button jslc = (Button) findViewById(R.id.jslc);
        jslc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, NumActivity.class));
            }
        });

        RelativeLayout rl1 = (RelativeLayout) findViewById(R.id.rl1);
        rl1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        RelativeLayout rl2 = (RelativeLayout) findViewById(R.id.rl2);
        rl2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String num = (String) SharedPreferencesUtil.get(MainActivity.this, "num", "");
                String add = (String) SharedPreferencesUtil.get(MainActivity.this, "add", "");
                if (!TextUtils.isEmpty(num) && !TextUtils.isEmpty(add)) {
                    startActivity(new Intent(MainActivity.this, XscxActivity.class));
                } else {
                    startActivity(new Intent(MainActivity.this, Num_Xscx_Activity.class));
                }
//                ToastUtil.showToast(MainActivity.this, "敬请期待");
            }
        });

        verticalScrollTV = (AutoVerticalScrollTextView) findViewById(R.id.textview_auto_roll);
        verticalScrollTV.setText(strings[0]);

        new Thread(){
            @Override
            public void run() {
                while (isRunning){
                    SystemClock.sleep(3000);
                    handler.sendEmptyMessage(199);
                }
            }
        }.start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            if (msg.what == 199) {
                verticalScrollTV.next();
                number++;
                verticalScrollTV.setText(strings[number%strings.length]);
            }

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isRunning=false;
    }
}
